﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Configuration;
using Microsoft.SharePoint;

namespace beyCoo4Sharepoint
{
    //this class contains all of the beyCooObjects that can call the beycoo webservice and get the results back
    class beyCooObject
    {
        public Hashtable entities = new Hashtable();
        public Hashtable rtf = new Hashtable();
        private bool useHttps;


        /// <summary>
        /// beyCooObject of type: *service: Entity Extraction *input type: string
        /// </summary>
        /// <param name="sourceText"></param>
        /// <param name="customerKey"></param>
        /// <param name="serviceKeyEE"></param>
        /// <param name="serverName"></param>
        /// <param name="xPathEntity"></param>
        public beyCooObject(string sourceText, string customerKey, string serviceKeyEE, string serverName, List<string> xPathEntity)
        {
            getHttps(serverName);
            string entitiesURL = "";
            //get the beyCooPlainURL and complete it
            if (useHttps)
            {
                entitiesURL = beyCooConstants.beyCooPlainURLhttps + customerKey + "/service-key/" + serviceKeyEE + "/beyCoo.xml";
            }
            else
            {
                entitiesURL = beyCooConstants.beyCooPlainURL + customerKey + "/service-key/" + serviceKeyEE + "/beyCoo.xml";
            }

            beyCooConnection beycooCon = new beyCooConnection();
            try
            {
                beyCooXmlSelecter beycooXml = new beyCooXmlSelecter(beycooCon.Connection(entitiesURL, sourceText, serverName), xPathEntity);
                this.entities = beycooXml.selectedEntity;
            }
            catch (Exception Exc)
            {
                //customerKey not applicable/empty, or other connection error
                beyCooLogger.LogError("beyCooError", "beyCooObject - " + Exc.Message + Exc.StackTrace);
            }
        }

        /// <summary>
        /// beyCooObject of type: *service: Relevant Term Finder *input type: string
        /// </summary>
        /// <param name="sourceText"></param>
        /// <param name="customerKey"></param>
        /// <param name="serviceKeyRTF"></param>
        /// <param name="serverName"></param>
        public beyCooObject(string sourceText, string customerKey, string serviceKeyRTF, string serverName, string xPathRTF)
        {
            getHttps(serverName);
            string rtfURL = "";
            //get the beyCooPlainURL and complete it
            if (useHttps)
            {
                rtfURL = beyCooConstants.beyCooPlainURLhttps + customerKey + "/service-key/" + serviceKeyRTF + "/beyCoo.xml";
            }
            else
            {
                rtfURL = beyCooConstants.beyCooPlainURL + customerKey + "/service-key/" + serviceKeyRTF + "/beyCoo.xml";
            }

            beyCooConnection beycooCon = new beyCooConnection();
            try
            {
                beyCooXmlSelecter beycooXml = new beyCooXmlSelecter(beycooCon.Connection(rtfURL, sourceText, serverName), xPathRTF);
                this.rtf = beycooXml.selectedRtf;
            }
            catch (Exception Exc)
            {
                //customerKey not applicable/empty, or other connection error
                beyCooLogger.LogError("beyCooError", "beyCooObject - " + Exc.Message + Exc.StackTrace);
            }

        }



        /// <summary>
        /// beyCooObject of type: *service: Entity Extraction *input type: file
        /// </summary>
        /// <param name="url"></param>
        /// <param name="file"></param>
        /// <param name="customerKey"></param>
        /// <param name="serviceKeyEE"></param>
        /// <param name="serverName"></param>
        /// <param name="xPathEntity"></param>
        public beyCooObject(string url, bool file, string customerKey, string serviceKeyEE, string serverName, List<string> xPathEntity)
        {
            getHttps(serverName);
            string fileEntitiesURL = "";
            //get the beyCooPlainURL and complete it
            if (useHttps)
            {
                fileEntitiesURL = beyCooConstants.beyCooFileURLhttps + customerKey + "/service-key/" + serviceKeyEE + "/beyCoo.xml";
            }
            else
            {
                fileEntitiesURL = beyCooConstants.beyCooFileURL + customerKey + "/service-key/" + serviceKeyEE + "/beyCoo.xml";
            }

            beyCooConnection beycooCon = new beyCooConnection();
            url = serverName + "/" + url;
            Uri uri = new Uri(url);
            string base64Text = beycooCon.File2Base64(uri.AbsoluteUri);

            try
            {
                beyCooXmlSelecter beycooXml = new beyCooXmlSelecter(beycooCon.Connection(fileEntitiesURL, base64Text, serverName), xPathEntity);
                this.entities = beycooXml.selectedEntity;
            }
            catch (Exception Exc)
            {
                //customerKey not applicable/empty, or other connection error
                beyCooLogger.LogError("beyCooError", "beyCooObject - " + Exc.Message + Exc.StackTrace);
            }

        }

        /// <summary>
        /// beyCooObject of type: *service: Relevant Term Finder *input type: file
        /// </summary>
        /// <param name="url"></param>
        /// <param name="file"></param>
        /// <param name="customerKey"></param>
        /// <param name="serviceKeyRTF"></param>
        /// <param name="serverName"></param>
        public beyCooObject(string url, bool file, string customerKey, string serviceKeyRTF, string serverName, string xPathRTF)
        {
            getHttps(serverName);
            string fileRtfURL = "";
            //get the beyCooPlainURL and complete it
            if (useHttps)
            {
                fileRtfURL = beyCooConstants.beyCooFileURLhttps + customerKey + "/service-key/" + serviceKeyRTF + "/beyCoo.xml";
            }
            else
            {
                fileRtfURL = beyCooConstants.beyCooFileURL + customerKey + "/service-key/" + serviceKeyRTF + "/beyCoo.xml";
            }

            beyCooConnection beycooCon = new beyCooConnection();
            url = serverName + "/" + url;
            Uri uri = new Uri(url);
            string base64Text = beycooCon.File2Base64(uri.AbsoluteUri);

            try
            {
                beyCooXmlSelecter beycooXml = new beyCooXmlSelecter(beycooCon.Connection(fileRtfURL, base64Text, serverName), xPathRTF);
                this.rtf = beycooXml.selectedRtf;
            }
            catch (Exception Exc)
            {
                //customerKey not applicable/empty, or other connection error
                beyCooLogger.LogError("beyCooError", "beyCooObject - " + Exc.Message + Exc.StackTrace);
            }

        }

        public bool getHttps(string serverName)
        {
            using (SPSite site = new SPSite(serverName))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    {
                        //check if Https is being requested
                        if (web.Properties.ContainsKey(beyCooConstants.beyCooUseHttpsChoice))
                        {
                            useHttps = Boolean.Parse(web.Properties[beyCooConstants.beyCooUseHttpsChoice].ToString());
                        }
                    }
                    return useHttps;
                }
            }
        }

    }
}
